package org.noote.shaky_remote;

import org.noote.libs.bluetooth.DeviceList_Activity;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class RobotRemote_Activity extends Activity {
	private static final int GETTING_DEVICE = 0;
	private static final int REQUEST_ENABLE_BT = 1;

	public static final String PREFS_NAME = "Robot_Remote";
	public static final String PREFS_NAME_LATEST_BTADDRESS = "latest_btaddress";

	BluetoothAdapter mBluetoothAdapter = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button buttonScan = (Button)findViewById(R.id.buttonScan);
        buttonScan.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
	           Intent intent = new Intent(getApplicationContext(), DeviceList_Activity.class);
	           startActivityForResult(intent, GETTING_DEVICE);
			}
        });
        Button buttonConnection = (Button)findViewById(R.id.buttonConnection);
        buttonConnection.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
	           LaunchConnection();
			}
        });

        GetAdapter();
        
        SharedPreferences settings = getSharedPreferences(RobotRemote_Activity.PREFS_NAME, 0);
		TextView textAddressDevice = (TextView)findViewById(R.id.textAddressDevice);
		textAddressDevice.setText(settings.getString(RobotRemote_Activity.PREFS_NAME_LATEST_BTADDRESS, textAddressDevice.getText().toString()));
    }

	protected void LaunchConnection() {
		
		Connect();
	}

	@Override
    public void onStart() {
        super.onStart();

		if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
		    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		    return;
		}
	}
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data)
	{
		switch(requestCode)
		{
		case REQUEST_ENABLE_BT:
			if(resultCode == RESULT_OK)
			{
				Connect();
			}
			break;
		case GETTING_DEVICE:
			if(resultCode == RESULT_OK)
			{
				String sAddressDevice = data.getStringExtra(DeviceList_Activity.EXTRA_DEVICE_ADDRESS);
				TextView textAddressDevice = (TextView)findViewById(R.id.textAddressDevice);
				textAddressDevice.setText(sAddressDevice);
				
				SharedPreferences settings = getSharedPreferences(RobotRemote_Activity.PREFS_NAME, 0);
				SharedPreferences.Editor settings_editor = settings.edit(); 
				settings_editor.putString(RobotRemote_Activity.PREFS_NAME_LATEST_BTADDRESS, sAddressDevice);
				settings_editor.commit();
			}
			break;
		}
	}

	private void GetAdapter() {
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
		    Toast.makeText(this, "No bluetooth adapter :(", Toast.LENGTH_LONG); 
			return;
		}
	}


    private void Connect() {
    	String sAddressDevice = ((TextView)findViewById(R.id.textAddressDevice)).getText().toString();
    	if(BluetoothAdapter.checkBluetoothAddress(sAddressDevice))
    	{
	    	Bundle bundle = new Bundle();
	    	bundle.putString(Remote_Activity.BT_ADDRESS_DEVICE, sAddressDevice);
    		Intent intent = new Intent(getApplicationContext(), Remote_Activity.class);
    		intent.putExtras(bundle);    		
            startActivity(intent);    		
    	}
	}
}